#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>

sem_t c, d;

void *Driver(void *arg) {
  sem_wait(&d);
  printf("开车\n");
  printf("正常行车\n");
  printf("到站停车\n");
  sem_post(&c);

  pthread_exit(0);
}

void *counter(void *arg) {
  printf("关车门\n");
  sem_post(&d);
  printf("售票\n");
  sem_wait(&c);
  printf("开车门\n");

  pthread_exit(0);
}

int main(int argc, char const *argv[]) {
  sem_init(&c, 0, 0);
  sem_init(&d, 0, 0);

  pthread_t tid1, tid2;

  pthread_create(&tid1, NULL, Driver, NULL);
  pthread_create(&tid2, NULL, counter, NULL);

  pthread_join(tid1, NULL);
  pthread_join(tid2, NULL);

  sem_destroy(&c);
  sem_destroy(&d);

  return 0;
}